package dao;

import entity.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Alexander
 */
public class UserJDBCDAO implements UserDAO<Connection> {

    private static UserJDBCDAO instance;
    private final String INSERT = "INSERT INTO Usser (id, login, name, passwd) VALUES (?, ?,?,?)";
    private final String FINDBYID = "SELECT * FROM Usser WHERE id = ?";
    private final String UPDATE = "UPDATE Usser SET LOGIN = ?, NAME = ?, PASSWD = ? WHERE ID = ?";
    private final String DELETE = "DELETE FROM Usser WHERE id = ?";
    private final String LIST_ALL = "SELECT * FROM USSER";
    private final String LOGIN = "SELECT * FROM Usser WHERE login = ? AND passwd = ?";
    
    private UserJDBCDAO() {
    }

    public static synchronized UserJDBCDAO getInstance() {
        if (instance == null) {
            instance = new UserJDBCDAO();
        }
        return instance;
    }

    @Override
    public void persist(User entity, Connection em) {
        
        PreparedStatement statement;

        try {
            statement = em.prepareStatement(INSERT);
            statement.setInt(1, entity.getId());
            statement.setString(2, entity.getLogin());
            statement.setString(3, entity.getName());
            statement.setString(4, entity.getPasswd());

            statement.execute();

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }

    @Override
    public User find(Object id, Connection em) {

        PreparedStatement statement;
        
        try {
            statement = em.prepareStatement(FINDBYID);
            statement.setInt(1, (int) id);

            ResultSet rs = statement.executeQuery();
            rs.next();

            User user = new User();
            user.setId(rs.getInt("ID"));
            user.setLogin(rs.getString("LOGIN"));
            user.setName(rs.getString("NAME"));
            user.setPasswd(rs.getString("PASSWD"));

            return (User) user;

        } catch (SQLException ex) {
            //Logger.getLogger(UserJDBCDAO.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

    }

    @Override
    public void update(User entity, Connection em) {

        PreparedStatement statement;

        try {
            statement = em.prepareStatement(UPDATE);
            statement.setString(1, entity.getLogin());
            statement.setString(2, entity.getName());
            statement.setString(3, entity.getPasswd());
            statement.setInt(4, entity.getId());
            statement.executeUpdate();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }

    @Override
    public void delete(Object id, Connection em) {
        
        PreparedStatement statement;

        try {
            statement = em.prepareStatement(DELETE);
            statement.setInt(1, (int)id);
            statement.executeUpdate();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }

    @Override
    public List<User> getList(Connection em) {

        PreparedStatement statement;
        List<User> usuarios = new ArrayList<>();

        try {
            statement = em.prepareStatement(LIST_ALL);
            ResultSet rs = statement.executeQuery();

            while (rs.next()) {                
                User user = new User();
                user.setId(rs.getInt("ID"));
                user.setLogin(rs.getString("LOGIN"));
                user.setName(rs.getString("NAME"));
                user.setPasswd(rs.getString("PASSWD"));
                usuarios.add(user);
            }

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } 
        return usuarios;
    }

    @Override
    public User login(User entity, Connection em) {
        PreparedStatement statement;
        
        try {
            statement = em.prepareStatement(LOGIN);
            statement.setString(1, entity.getLogin());
            statement.setString(2, entity.getPasswd());

            ResultSet rs = statement.executeQuery();
            rs.next();

            User user = new User();
            user.setId(rs.getInt("ID"));
            user.setLogin(rs.getString("LOGIN"));
            user.setName(rs.getString("NAME"));
            user.setPasswd(rs.getString("PASSWD"));

            return (User) user;

        } catch (SQLException ex) {
            //Logger.getLogger(UserJDBCDAO.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
}
